<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>重写右键点击菜单</title>
  <style>
    .wrapper ul {
      margin: 0;
      padding: 0;
      width: 100px;
      background-color: burlywood;
      list-style: none;
    }
    li:hover {
      background-color: coral;
    }
  </style>
</head>
<body>
  <div class="wrapper">
    <ul>
      <li>一级</li>
      <li>二级</li>
      <li>三级</li>
    </ul>
  </div>

  <script>
    const wrapper = document.querySelector('.wrapper')
    const lists = document.querySelectorAll('li')


    wrapper.style.display = 'none'
    wrapper.style.position = 'relative'
    lists.forEach(list => {
      list.addEventListener('onmouseenter', () => this.classList.add('active'))
      list.addEventListener('onmouseleave', () => this.classList.remove('active'))
    })

    document.oncontextmenu = (e) => {
      wrapper.style.display = 'block'
      wrapper.style.left = e.clientX + 'px'
      wrapper.style.top = e.clientY + 'px'
      // 取消默认的鼠标点击事件
      return false
    }
    document.onclick = (e) => {
      if(e.target.nodeName !== 'LI') {
        wrapper.style.display = 'none'
      } else {
        console.log(e.target.textContent)
      }
    }
  </script>
</body>
</html>